package com.hknc.btscale.utils;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

public class DataConvertUtils {
    /**
     * 将int数值转换为占四个字节的byte数组，本方法适用于(高位在前，低位在后)的顺序。  和bytesToInt2（）配套使用
     */
    public static byte[] intToBytes2(int value) {
        byte[] src = new byte[3];
//        src[0] = (byte) ((value >> 24) & 0xFF);
        src[0] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[2] = (byte) (value & 0xFF);
        return src;
    }

    /**
     * 四字节
     *
     * @param value
     * @return
     */
    public static byte[] intTo4Bytes(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    /**
     * 2字节
     *
     * @param value
     * @return
     */
    public static byte[] intTo2Bytes(int value) {
        byte[] src = new byte[2];
//        src[0] = (byte) ((value >> 24) & 0xFF);
//        src[0] = (byte) ((value >> 16) & 0xFF);
        src[0] = (byte) ((value >> 8) & 0xFF);
        src[1] = (byte) (value & 0xFF);
        return src;
    }

    /**
     * 字节数组转string
     *
     * @param bytes
     * @return
     */
    public static String bytesToString(byte[] bytes) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        // 一个字节对应两个16进制数，所以长度为字节数组乘2
        char[] resultCharArray = new char[bytes.length * 2];
        int index = 0;
        for (byte b : bytes) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        String result = new String(resultCharArray);
        return result;
    }

    /**
     * 十六进制字符串转int类型
     *
     * @param hexStr
     * @return
     */
    public static int hexToInt(String hexStr) {
        BigInteger bigInteger = new BigInteger(hexStr, 16);
        return bigInteger.intValue();
    }

    /**
     * byte数组转为int
     *
     * @return
     */
    public static double bytesToInt(byte[] bytes) {
        return hexToInt(bytesToString(bytes)) / 1000.0;
    }

    /**
     * byte数组转为int
     *
     * @return
     */
    public static int bytesToIntNormal(byte[] bytes) {
        return hexToInt(bytesToString(bytes));
    }

    /**
     * byte数组转为int
     *
     * @return
     */
    public static double bytesToInt_100(byte[] bytes) {
        return hexToInt(bytesToString(bytes)) / 100.0;
    }

    /**
     * int转string保留3位小数
     *
     * @return
     */
    public static String intToStr(double a) {
        String str = String.format("%.3f", a);
        return str;
    }

    /**
     * int转string保留2位小数
     *
     * @return
     */
    public static String intToStr_2(double a) {
        String str = String.format("%.2f", a);
        return str;
    }

    /**
     * 结果保留三位小数
     *
     * @return
     */
    public static String bytesToStr_3(byte[] bytes) {
        return intToStr(bytesToInt(bytes));
    }


    /**
     * 结果保留两位小数
     *
     * @return
     */
    public static String bytesToStr_2(byte[] bytes) {
        return intToStr_2(bytesToInt_100(bytes));
    }


    //字节数组转化为int
    public static int byteArrayToInt(byte[] arr) {
        int result = 0;
        ByteArrayInputStream bis = new ByteArrayInputStream(arr);
        DataInputStream dis = new DataInputStream(bis);
        try {
            result = dis.readInt();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int byteArrayToIntNormal(byte[] bytes) {

        int value = 0;
        //由高位到低位
        for (int i = 0; i < 2; i++) {
            int shift = (2 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;//往高位游
        }
        return value;
    }

    public static int byteArrayToIntNormal1(byte[] bytes) {

        int value = 0;
        //由高位到低位
        for (int i = 0; i < 3; i++) {
            int shift = (3 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;//往高位游
        }
        return value;
    }

    public static int bytesToIntHutool(byte[] bytes) {
        return bytes[1] & 0xFF | //
                (bytes[0] & 0xFF) << 8; //
    }


    public static int bytesToIntLittle(byte[] src, int offset) {
//        int value = src[offset] & 255 | (src[offset + 1] & 255) << 8 | (src[offset + 2] & 255) << 16 | (src[offset + 3] & 255) << 24;
        return src[offset] & 255 << 24 | (src[offset + 1] & 255) << 16 | (src[offset + 2] & 255) << 8 | src[offset + 3] & 255;
    }
}
